<template>
  <view>
    <!-- 顶部搜索区 -->
    <view class="search-box">
      <my-search @click="goToSearch"></my-search>
    </view>
    <!-- 轮播图区域 -->
    <swiper indicator-dots autoplay circular :interval='2000' :duration='1000'>
      <swiper-item v-for='(item,index) in swiperList' :key='item.goods_id'>
        <!-- 轮播项 -->
        <navigator class='swiper-item' :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image class="swiper-img" :src='item.image_src'></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航栏区域 -->
    <view class="nav-box">
      <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickSwitchTab(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层数据 -->
    <view class="floor">
      <!-- 顶部标题区 -->
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <image :src="item.floor_title.image_src"></image>
        <!-- 图片显示区 -->
        <view class="pic-box">
          <!-- 左侧图片 -->
          <navigator class="pic-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src"
              :style="{width: item.product_list[0].image_width + 'rpx',height:'193px'}">
            </image>
          </navigator>
          <!-- 右侧图片 -->
          <view class="pic-right-box">
            <navigator class="pic-right" v-for="(val,i) in item.product_list" v-if="i!==0" :key="i" :url="val.url">
              <image :src="val.image_src" :style="{width: val.image_width + 'rpx',height:'95px'}"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import tabBarBadge from '@/mixins/tabBarBadge.js'
  export default {
    mixins: [tabBarBadge],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图信息
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) {
          return uni.$showMsg('轮播图加载失败...')
        } else {
          this.swiperList = res.message
        }
        // console.log('res', res)
      },
      // 获取导航栏信息
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) {
          return uni.$showMsg('分类导航栏加载失败...')
        } else {
          this.navList = res.message
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) {
          return uni.$showMsg('楼层加载失败...')
        } else {
          // 图片路径数据提取
          res.message.forEach(item => {
            item.product_list.forEach(val => {
              val.url = '/subpkg/goods_list/goods_list?' + val.navigator_url.split('?')[1]
            })
          })
          this.floorList = res.message
          console.log(this.floorList)
        }
      },
      // 导航区域点击切换tab
      navClickSwitchTab(item) {
        // console.log('zz',item)
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      goToSearch() {
        uni.navigateTo({
          // 前往搜索界面
          url: "/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  // 轮播图样式
  swiper {
    height: 300rpx;
  }

  .swiper-item,
  .swiper-img {
    height: 100%;
    width: 100%;
  }

  // 分类导航栏样式
  .nav-box {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-img {
      width: 140rpx;
      height: 128rpx;
    }
  }

  // 楼层图片样式
  .floor-item image {
    height: 60rpx;
    width: 100%;
    display: flex;
    margin-top: 3px;
  }

  .pic-box {
    display: flex;
    padding-left: 10rpx;
  }

  .pic-right-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  // 顶部搜索
  .search-box {
    // 设置吸顶效果
    position: sticky;
    top: 0;
    z-index: 1;
  }
</style>
